package com.ruoyi.system.controller;/**
 * @InterfaceName: BusinessUserController
 * @Description:
 * @Author: YourName
 * @Date: 2023-04-12
 */

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.ip.IpUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteKjyService;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysRole;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @InterfaceName: BusinessUserController
 * @Description:
 * @Author: lilirui
 * @Date: 2023-04-12
 */
@Api(tags = "讲师")
@RestController
@RequestMapping("")
public class BusinessUserController extends BaseController {
    @Autowired
    private ISysUserService userService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private RemoteKjyService remoteKjyService;

    @GetMapping("/getfreeuser")
    public AjaxResult getfreeuser(Long list) {
        SysUser sysUser = userService.selectUserById(list);
        LoginUser loginUser = new LoginUser();
        loginUser.setSysUser(sysUser);
        return AjaxResult.success(tokenService.createToken(loginUser));
    }

    /**
     * 获取讲师列表
     */
    @ApiOperation("讲师")
    @GetMapping("/lecturer")
    public TableDataInfo lecturerList(SysUser user) {
        user.setRoleId(100l);
        user.setState("2");
        startPage();
        List<SysUser> list = userService.selectAllocatedList(user);
        TableDataInfo dataTable = getDataTable(list);
        return dataTable;
    }

    @ApiOperation("讲师")
    @PostMapping("/lecturer")
    public AjaxResult lecturerAdd(@RequestBody SysUser user) {
        user.setRoleId(100l);
        R<?> registerResult = remoteUserService.registerUserInfo(user, SecurityConstants.INNER);

        if (R.FAIL == registerResult.getCode()) {
            throw new ServiceException(registerResult.getMsg());
        }
        int i = userService.insertUser(user);

        return AjaxResult.success(i > 0);
    }

    @ApiOperation("updateInfo")
    @PutMapping("/updateInfo")
    public AjaxResult updateInfo(@RequestBody SysUser user) {
        remoteKjyService.updateAchievementNickName(user,SecurityConstants.INNER);
        remoteKjyService.updateDemandNickName(user,SecurityConstants.INNER);
        String state = user.getState();
        // 如果用户状态为未认证，则将其修改为审核中
        if (state.equals("0")) {
            user.setState("1");
        }
        int i = userMapper.updateUser(user);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        loginUser.setSysUser(userMapper.selectUserById(user.getUserId()));
        loginUser.setUsername(user.getUserName());
        loginUser.setIpaddr(IpUtils.getIpAddr());
        tokenService.refreshToken(loginUser);
        return AjaxResult.success(i > 0);
    }

    @ApiOperation("讲师")
    @DeleteMapping("/lecturer")
    public AjaxResult lecturerDelete(Long Uid) {
        SysUser data = userService.selectUserById(Uid);
        if (data.getRoleId() == 100l) {
            int i = userService.deleteUserById(Uid);
            return AjaxResult.success(i > 0);
        } else {
            throw new ServiceException("未找到相关信息");
        }
    }

    @ApiOperation("讲师")
    @GetMapping("/lecturer/getById")
    public AjaxResult lecturerGetById(Long Uid) {
        SysUser data = userService.selectUserById(Uid);
        for (SysRole role : data.getRoles()) {
            if (role.getRoleId() == 100l) {
                return AjaxResult.success(data);
            }
        }
        throw new ServiceException("未找到相关信息");
    }

    /**
     * 获取专家列表
     */
    @ApiOperation("专家")
    @GetMapping("/technician")
    public TableDataInfo technicianList(SysUser user) {
        user.setRoleId(102l);
        user.setState("2");
        startPage();
        List<SysUser> list = userService.selectAllocatedList(user);
        TableDataInfo dataTable = getDataTable(list);
        return dataTable;
    }

    @ApiOperation("专家")
    @PostMapping("/technician")
    public AjaxResult technicianAdd(@RequestBody SysUser user) {
        user.setRoleId(102l);
        R<?> registerResult = remoteUserService.registerUserInfo(user, SecurityConstants.INNER);

        if (R.FAIL == registerResult.getCode()) {
            throw new ServiceException(registerResult.getMsg());
        }
        int i = userService.insertUser(user);

        return AjaxResult.success(i > 0);
    }


    @ApiOperation("专家")
    @DeleteMapping("/technician")
    public AjaxResult technicianDelete(Long Uid) {
        SysUser data = userService.selectUserById(Uid);
        if (data.getRoleId() == 102l) {
            int i = userService.deleteUserById(Uid);
            return AjaxResult.success(i > 0);
        } else {
            throw new ServiceException("未找到相关信息");
        }
    }

    @ApiOperation("专家")
    @GetMapping("/technician/getById")
    public AjaxResult technicianGetById(Long Uid) {
        SysUser data = userService.selectUserById(Uid);
        for (SysRole role : data.getRoles()) {
            if (role.getRoleId() == 102l) {
                return AjaxResult.success(data);
            }
        }
        throw new ServiceException("未找到相关信息");
    }

    /**
     * 获取讲师列表
     */
    @ApiOperation("企业人员")
    @GetMapping("/employee")
    public TableDataInfo employeeList(SysUser user) {
        user.setRoleId(101l);
        startPage();
        List<SysUser> list = userService.selectAllocatedList(user);
        TableDataInfo dataTable = getDataTable(list);
        return dataTable;
    }

    @ApiOperation("企业人员")
    @PostMapping("/employee")
    public AjaxResult employeeAdd(@RequestBody SysUser user) {
        user.setRoleId(101l);
        R<?> registerResult = remoteUserService.registerUserInfo(user, SecurityConstants.INNER);

        if (R.FAIL == registerResult.getCode()) {
            throw new ServiceException(registerResult.getMsg());
        }
        int i = userService.insertUser(user);

        return AjaxResult.success(i > 0);
    }


    @ApiOperation("企业人员")
    @DeleteMapping("/employee")
    public AjaxResult employeeDelete(Long Uid) {
        SysUser data = userService.selectUserById(Uid);
        if (data.getRoleId() == 101l) {
            int i = userService.deleteUserById(Uid);
            return AjaxResult.success(i > 0);
        } else {
            throw new ServiceException("未找到相关信息");
        }
    }

    @ApiOperation("企业人员")
    @GetMapping("/employee/getById")
    public AjaxResult employeeGetById(Long Uid) {
        SysUser data = userService.selectUserById(Uid);
        for (SysRole role : data.getRoles()) {
            if (role.getRoleId() == 101l) {
                return AjaxResult.success(data);
            }
        }
        throw new ServiceException("未找到相关信息");
    }

    /**
     * 获取创业导师列表
     */
    @ApiOperation("创业导师")
    @GetMapping("/mentors")
    public TableDataInfo mentorsList(SysUser user) {
        user.setRoleId(103L);
        user.setState("2");
        startPage();
        List<SysUser> list = userService.selectAllocatedList(user);
        TableDataInfo dataTable = getDataTable(list);
        return dataTable;
    }
    @ApiOperation("创业导师")
    @GetMapping("/mentors/getById")
    public AjaxResult mentorsGetById(Long Uid) {
        SysUser data = userService.selectUserById(Uid);
        for (SysRole role : data.getRoles()) {
            if (role.getRoleId() == 103L) {
                return AjaxResult.success(data);
            }
        }
        throw new ServiceException("未找到相关信息");
    }

    /**
     * 获取成果转化专员列表
     */
    @ApiOperation("成果转化专员")
    @GetMapping("/specialists")
    public TableDataInfo specialistsList(SysUser user) {
        user.setRoleId(104L);
        user.setState("2");
        startPage();
        List<SysUser> list = userService.selectAllocatedList(user);
        TableDataInfo dataTable = getDataTable(list);
        return dataTable;
    }
    @ApiOperation("成果转化专员")
    @GetMapping("/specialists/getById")
    public AjaxResult specialistsGetById(Long Uid) {
        SysUser data = userService.selectUserById(Uid);
        for (SysRole role : data.getRoles()) {
            if (role.getRoleId() == 104L) {
                return AjaxResult.success(data);
            }
        }
        throw new ServiceException("未找到相关信息");
    }
}
